package com.example.administrator.liveapp.fragment;

import android.content.Intent;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.activity.AccountManagementPage;
import com.example.administrator.liveapp.activity.AllOrdersPage;
import com.example.administrator.liveapp.activity.BindingPhonePage;
import com.example.administrator.liveapp.activity.LoginPage;
import com.example.administrator.liveapp.activity.MessageRemind;
import com.example.administrator.liveapp.activity.ModifyPersonalInformationPage;
import com.example.administrator.liveapp.activity.MyArticle;
import com.example.administrator.liveapp.activity.MyComment;
import com.example.administrator.liveapp.activity.MyReservationPage;
import com.example.administrator.liveapp.activity.ReceivingAddressPage;
import com.example.administrator.liveapp.activity.ShoppingCart;
import com.example.administrator.liveapp.activity.WxLoginPage;
import com.example.administrator.liveapp.base.BaseFragment;
import com.example.administrator.liveapp.bean.MyFragmentBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.utils.GlideImgManager;
import com.example.administrator.liveapp.utils.SharedPrefsUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 我的Fragment
 */

public class MyFragment extends BaseFragment {
    private View view;
    private TextView my_user_name;//用户昵称
    private TextView my_money;
    private TextView my_Individuality_signature;//个性签名
    private RelativeLayout account_management;//账号管理
    private TextView login_immediately;//立即登录
    private RelativeLayout user_information;//个人信息
    private RelativeLayout my_reservation;//我的预约
    private RelativeLayout All_Orders;//全部订单
    private RelativeLayout shopping_cart;//我的购物车
    private RelativeLayout my_article;//我的文章
    private RelativeLayout my_comment;//我的评论
    private RelativeLayout my_receiving_address;//收货地址
    private RelativeLayout my_Message_remind;//消息提醒
    private ImageView dot;
    private MyFragmentBean bean;
    private ImageView my_head_portrait;
    SharedPrefsUtil sp;


    @Override
    protected View initView() {
        sp = new SharedPrefsUtil();
        view = View.inflate(getContext(), R.layout.myfragment_layout, null);
        my_user_name = (TextView) view.findViewById(R.id.my_user_name);
        my_money = (TextView) view.findViewById(R.id.my_money);
        my_Individuality_signature = (TextView) view.findViewById(R.id.my_Individuality_signature);
        my_head_portrait = (ImageView) view.findViewById(R.id.my_head_portrait);
        account_management = (RelativeLayout) view.findViewById(R.id.account_management);
        login_immediately = (TextView) view.findViewById(R.id.login_immediately);
        user_information = (RelativeLayout) view.findViewById(R.id.user_information);
        my_reservation = (RelativeLayout) view.findViewById(R.id.my_reservation);
        All_Orders = (RelativeLayout) view.findViewById(R.id.All_Orders);
        shopping_cart = (RelativeLayout) view.findViewById(R.id.shopping_cart);
        my_comment = (RelativeLayout) view.findViewById(R.id.my_comment);
        my_article = (RelativeLayout) view.findViewById(R.id.my_article);
        my_receiving_address = (RelativeLayout) view.findViewById(R.id.my_receiving_address);
        my_Message_remind = (RelativeLayout) view.findViewById(R.id.my_Message_remind);
        dot = (ImageView) view.findViewById(R.id.dot);
        listener();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        MyPageData();
    }

    private void MyPageData() {
        Log.e("bean",ApplicationUtils.getInstance().loginBean+"");
        if (ApplicationUtils.getInstance().loginBean != null) {
            user_information.setVisibility(View.VISIBLE);
            String url = AppUrl.URLS + AppUrl.MyPageURL;
            Map<String, String> map = new HashMap<>();
            Log.e("bean",ApplicationUtils.getInstance().loginBean+"");
            map.put("consumerId", ApplicationUtils.getInstance().loginBean.getConsumerid());
            XUtils.Get(url, map, new MyCallBack<String>() {
                @Override
                public void onSuccess(String result) {
                    super.onSuccess(result);
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    String code = jsonObject.optString("result");
                    if (code.equals("true")) {
                        login_immediately.setVisibility(View.GONE);
                        user_information.setVisibility(View.VISIBLE);
                        Gson gson = new Gson();
                        bean = gson.fromJson(result, MyFragmentBean.class);
                        if(bean.getConsumerphoto()!=null&&!bean.getConsumerphoto().equals("")){
                            String str = bean.getConsumerphoto();
                            String photo = str.substring(0,4);
                            if(photo.equals("http")){
                                GlideImgManager.glideLoader(getContext(),bean.getConsumerphoto(), R.mipmap.consumer, R.mipmap.consumer, my_head_portrait, 0);//Glide圆形图片工具类
                            }else{
                                GlideImgManager.glideLoader(getContext(), AppUrl.URLS + bean.getConsumerphoto(), R.mipmap.consumer, R.mipmap.consumer, my_head_portrait, 0);//Glide圆形图片工具类
                            }
                        }
                        if(bean.getIf_contract().equals("1")){
                            my_money.setText("文章获得赞赏"+bean.getReward()+"元");
                        }else{
                            my_money.setText("可与半书房签约获得文章赞赏");
                        }
                        if (!bean.getConsumername().equals("")) {
                            my_user_name.setText(bean.getConsumername());
                        } else {
                            my_user_name.setText("新用户");
                        }
                        if (!bean.getSign().equals("")) {
                            my_Individuality_signature.setText(bean.getSign());
                        } else {
                            my_Individuality_signature.setText("此人很懒,还没有写个性签名");
                        }
                        messageAlertState();
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    super.onError(ex, isOnCallback);
                }
            });
        } else {
            login_immediately.setVisibility(View.VISIBLE);
            user_information.setVisibility(View.GONE);
        }
    }

    private void messageAlertState() {
        String url = AppUrl.URLS + AppUrl.MessageAlertStateUrl;
        Map<String,String> map = new HashMap<>();
        if(ApplicationUtils.getInstance().loginBean==null) {
            startActivity(new Intent(getContext(),WxLoginPage.class));
            getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
            Toast.makeText(mContext, "用户未登录", Toast.LENGTH_SHORT).show();
            return;
        }
        map.put("consumerId", ApplicationUtils.getInstance().loginBean.getConsumerid());
        if(ApplicationUtils.getInstance().loginBean.getConsumertel()!=null){
            map.put("consumerTel", ApplicationUtils.getInstance().loginBean.getConsumertel().toString());
        }else if(ApplicationUtils.getInstance().loginBean.getWxopenid()!=null){
            map.put("openid", ApplicationUtils.getInstance().loginBean.getWxopenid().toString());
        }
        XUtils.Get(url, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(result);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String code = jsonObject.optString("code");
                if (code.equals("4814")||code.equals("48117")) {
                    dot.setVisibility(View.VISIBLE);
                } else {
                    dot.setVisibility(View.GONE);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void listener() {
        login_immediately.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getContext(), WxLoginPage.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
            }
        });

        user_information.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ApplicationUtils.getInstance().loginBean == null) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), WxLoginPage.class);
                    startActivity(intent);
                    getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("consumerId", ApplicationUtils.getInstance().loginBean.getConsumerid());
                intent.setClass(getContext(), ModifyPersonalInformationPage.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
            }
        });

        account_management.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getContext(), AccountManagementPage.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
            }
        });
        my_reservation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ApplicationUtils.getInstance().loginBean == null) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), WxLoginPage.class);
                    startActivity(intent);
                    getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("consumerId", ApplicationUtils.getInstance().loginBean.getConsumerid());
                intent.putExtra("consumerName", bean.getConsumername());
                intent.setClass(getContext(), MyReservationPage.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
            }
        });
        All_Orders.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ApplicationUtils.getInstance().loginBean == null) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), WxLoginPage.class);
                    startActivity(intent);
                    getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
                    return;
                }
                Intent intent = new Intent();
                intent.setClass(getContext(), AllOrdersPage.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);

            }
        });
        shopping_cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ApplicationUtils.getInstance().loginBean == null) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), WxLoginPage.class);
                    startActivity(intent);
                    getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
                    return;
                }
                Intent intent = new Intent();
                intent.setClass(getContext(), ShoppingCart.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);

            }
        });
        my_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ApplicationUtils.getInstance().loginBean == null) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), WxLoginPage.class);
                    startActivity(intent);
                    getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("consumerId", ApplicationUtils.getInstance().loginBean.getConsumerid());
                intent.setClass(getContext(), MyComment.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
            }
        });
        my_article.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ApplicationUtils.getInstance().loginBean == null) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), WxLoginPage.class);
                    startActivity(intent);
                    getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("consumerId", ApplicationUtils.getInstance().loginBean.getConsumerid());
                intent.setClass(getContext(), MyArticle.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);

            }
        });
        my_receiving_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ApplicationUtils.getInstance().loginBean == null) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), WxLoginPage.class);
                    startActivity(intent);
                    getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
                    return;
                }
                Intent intent = new Intent();
                intent.setClass(getContext(), ReceivingAddressPage.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);

            }
        });
        my_Message_remind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ApplicationUtils.getInstance().loginBean == null) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), WxLoginPage.class);
                    startActivity(intent);
                    getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
                    return;
                }
                Intent intent = new Intent();
                intent.setClass(getContext(), MessageRemind.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
    }
}
